package com.service.impl;

import com.dao.TourDao;
import com.dao.impl.TourDaoImpl;
import com.pojo.Page;
import com.pojo.Tour;
import com.service.TourService;

import java.util.List;

/**
 * @Author hjb
 * @Version 1.0
 * @Contact 1754729389@qq.com
 */
public class TourServiceImpl implements TourService {

    TourDao tourDao = new TourDaoImpl();

    @Override
    public void addTour(Tour tour) {

        tourDao.addTour(tour);

    }

    @Override
    public void deleteTourById(Integer id) {

        tourDao.delectTour(id);

    }

    @Override
    public void updateTour(Tour tour) {

        tourDao.updateTour(tour);

    }

    @Override
    public Tour queryTourById(Integer id) {
        return tourDao.queryTour(id);
    }

    @Override
    public List<Tour> queryTours() {
        return tourDao.queryTours();
    }

    @Override
    public Page<Tour> page(int pageNo, int pageSize) {

        Page<Tour> page = new Page<>();

        //设置当前页码
        page.setPageNo(pageNo);

        //设置每个页面显示数量
        page.setPageSize(pageSize);

        //设置总记录数
        Integer pageTotalCount = tourDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);

        //设置总页码数
        Integer pageTotal = pageTotalCount / pageSize;
        if(pageTotalCount % pageSize > 0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);

        //设置当前页数据
        int beginNo = (page.getPageNo() - 1) * pageSize;
        List<Tour> items = tourDao.queryForPageItems(beginNo,pageSize);
        page.setItems(items);

        return page;
    }

    @Override
    public Page<Tour> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Tour> page = new Page<>();

        //设置当前页码
        page.setPageNo(pageNo);

        //设置每个页面显示数量
        page.setPageSize(pageSize);

        //设置总记录数
        Integer pageTotalCount = tourDao.queryForPageTotalCountByPrice(min,max);
        page.setPageTotalCount(pageTotalCount);

        //设置总页码数
        Integer pageTotal = pageTotalCount / pageSize;
        if(pageTotalCount % pageSize > 0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);

        //设置当前页数据
        int beginNo = (page.getPageNo() - 1) * pageSize;
        List<Tour> items = tourDao.queryForPageItemsByPrice(beginNo,pageSize,min,max);
        page.setItems(items);

        return page;
    }
}
